//
//  JXHistoricalVisitManager.m
//

#import "JXHistoricalVisitManager.h"
#import "JXItemModel.h"

@interface JXHistoricalVisitManager()
@end

@implementation JXHistoricalVisitManager

+ (instancetype)sharedInstance {
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (void)clearHistory {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kJXHistoryGood];
}

- (void)setIsSendItem:(BOOL)isSendItem {
    _isSendItem = isSendItem;
}

- (NSArray *)loadFakeHistoricalGoods {
    // 历史访问列表
    NSArray *indexArray = [[NSUserDefaults standardUserDefaults] objectForKey:kJXHistoryGood];
    NSMutableArray *goodArray = [NSMutableArray array];
    for (NSNumber *item in indexArray) {
        if ([goodArray containsObject:[self goodInfoAtIndex:item.longValue]]) {
            [goodArray removeObject:[self goodInfoAtIndex:item.longValue]];
        }
        [goodArray addObject:[self goodInfoAtIndex:item.longValue]];
    }
    NSMutableArray *browses = [NSMutableArray array];

    for (NSDictionary *object in goodArray) {
        JXItemModel *good = [[JXItemModel alloc] init];
        NSString *imagename = [object objectForKey:GoodImageName];
        good.image = JXImage(imagename);
        good.name = [object objectForKey:GoodTitle];
        good.info = [object objectForKey:GoodDetail];
        [browses addObject:good];
    }
    return browses;

}

- (void)setHistorys:(NSMutableArray *)historys {
    [[NSUserDefaults standardUserDefaults] setObject:historys forKey:kJXHistoryGood];

}

- (NSMutableArray *)historys {
    return [[NSUserDefaults standardUserDefaults] objectForKey:kJXHistoryGood];
}

- (NSDictionary *)goodInfoAtIndex:(NSInteger)index {
    NSDictionary *goodInfoDic = [NSDictionary dictionary];
    switch (index) {
        case 1:
            goodInfoDic = @{ GoodTitle : @"红米Note2移动4G手机双卡双待",
                             GoodDetail : @"¥699 16G",
                             GoodImageName : @"goodDetail1"};
            break;
        case 2:
            goodInfoDic =
            @{ GoodTitle : @"Apple iPhone 5s 4G手机",
               GoodDetail : @" ¥ 2488 16G 金色",
               GoodImageName : @"goodDetail2"};
            break;
        case 3:
            goodInfoDic =
            @{ GoodTitle : @"魅蓝note2 真8核 4G双卡双待",
               GoodDetail : @" ¥829 16G ",
               GoodImageName : @"goodDetail3"};
            break;
        default:
            break;
    }
    return goodInfoDic;
}

@end
